<template>
  <div>
    <!-- 导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/SystemHomePage' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item>房屋信息</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="40">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getSellList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGrabble"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 用户列表区 -->
      <el-table :data="currentPageData" stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="位置" prop="position"></el-table-column>
        <el-table-column label="房源" prop="resource"></el-table-column>
        <el-table-column label="面积/m^2" prop="area"></el-table-column>
        <el-table-column label="户型" prop="houseType"></el-table-column>
        <el-table-column label="状态" prop="state"></el-table-column>
        <el-table-column label="价格(m^2/W)" prop="price"></el-table-column>
        <el-table-column label="操作">
          <el-badge class="item" slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              @click="removeHouseById(scope.row._id)"
              >恢复</el-button
            >
          </el-badge>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-tag type="success">总成交价格:{{ this.totalMoney }}W</el-tag>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pageNum: 1, // 当前页数，默认为1
        pageSize: 5, // 每页显示数量
      },
      SellList: [], //所有数据
      currentPageData: [], //当前页显示内容
      total: 1, //页面所有条数
      FuzzyQuery: null,
      totalMoney: 0,
      totalPrice: [],
      totalArea: [],
      univalent: [],
      addForm: {
        position: null, //位置
        resource: null, //房源
        area: null, //面积
        houseType: null, //户型
        state: null, //状态
        price: null, //价格
        picture: null,
      }, //添加表单
    };
  },
  mounted() {
    this.getSellList();
    this.getMoney();
  },
  methods: {
    //显示用户列表
    async getSellList() {
      const { data: result } = await this.$http.get("SellList", {
        params: this.queryInfo,
      });
      if (!result.ret) return this.$message.error("获取房屋列表失败");
      this.SellList = result.data;
      this.currentPageData = result.data;
      //显示共多少条
      this.total = this.SellList.length;
      this.handleSizeChange();
    },
    //房屋查询
    async getGrabble() {
      const getGrabbleValue = await this.$http.post("GrabbleSell", {
        params: this.queryInfo.query,
      });
      console.log(getGrabbleValue);
      const FuzzyQuerydata = getGrabbleValue.data.data.GrabbleSell;
      if (FuzzyQuerydata != "") {
        this.currentPageData = FuzzyQuerydata;
      } else {
        this.currentPageData = this.SellList;
      }
    },
    //监听pageSize改变是事件
    handleSizeChange(newSize) {
      //获取当前页面共多少条信息
      if (newSize) {
        this.queryInfo.pageSize = newSize;
      } else {
        this.queryInfo.pageSize;
      }
      let begin = (this.queryInfo.pageNum - 1) * this.queryInfo.pageSize;
      let end = this.queryInfo.pageNum * this.queryInfo.pageSize;
      this.currentPageData = this.SellList.slice(begin, end);
    },
    //监听页码值改变是事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.handleSizeChange(this.pageSize);
    },
    //获取销售额
    async getMoney() {
      const { data: result } = await this.$http.get("SellList", {
        params: this.queryInfo,
      });
      for (let i = 0; i < result.data.length; i++) {
        this.totalPrice.push(result.data[i].price);
        this.totalArea.push(result.data[i].area);
      }
      for (
        let j = 0, k = 0;
        j < this.totalPrice.length, k < this.totalArea.length;
        j++, k++
      ) {
        this.univalent.push(this.totalPrice[j] * this.totalArea[k]);
        this.totalMoney += this.univalent[j];
      }
      console.log(this.totalMoney);
    },
    // 获取想要删除值的id
    async removeHouseById(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该房屋, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      //如果用户删除返回字符串confirm,如果用户不删除则返回字符串cancel
      if (confirmResult !== "confirm") {
        return this.$Message.info("您已取消该操作");
      } else {
          this.updateHouseById(id);
        //通过id进行查找删除
        const { data: result } = await this.$http.delete("removeSell", {
          data: { id },
        });
        if (result.errorCode !== 0) {
          return this.$Message.error("删除失败");
        } else {
          this.$Message.success("删除成功");
          this.getSellList();
        }
      }
    },
    //根据ID获取想要的值
    async updateHouseById(id) {
      const { data: result } = await this.$http.put("ListOneSell", { id: id });
      (this.addForm.position = result.position),
        (this.addForm.resource = result.resource),
        (this.addForm.area = result.area),
        (this.addForm.houseType = result.houseType),
        (this.addForm.picture = result.picture),
        (this.addForm.price = result.price),
        (this.addForm.state =  "未出售")
         this.addHouse();
    },
    //添加房屋
    async addHouse() {
      const { data: result } = await this.$http.post("HouseSignup", this.addForm);
      console.log(result);
      if (result == undefined) {
        this.$Message.error("房屋添加失败");
      } else {
        this.$Message.success("房屋添加成功");
      }
    },
  },
};
</script>
<style scoped>
.text {
  font-size: 14px;
}
.item {
  margin-top: 10px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
.el-tag {
  font-size: 20px;
  float: right;
  margin-top: -2.5%;
  margin-right: 8%;
}
</style>